/*
 * 本文件定义菜单内容
 * 以供外部使用
 **/

import { defineStore } from 'pinia';

// 定义菜单项的类型
interface MenuItem {
  name: string; // 菜单名称
  isDirectory: boolean; // 是否是目录
  parent?: string; // 所属目录（仅对菜单项有效）
}

export const useMenuBackStageStore = defineStore('menuBackStage', {
  state: () => ({
    menus: {
      '/front': { name: '前台门户', isDirectory: true },
      '/front/view': { name: '前台首页', isDirectory: false, parent: '/front' },
      '/front/announcement': { name: '公告信息', isDirectory: false, parent: '/front' },

      '/back/announcement': { name: '公告信息', isDirectory: true },
      '/back/announcement/manage': { name: '公告管理', isDirectory: false, parent: '/back/announcement' },

      '/back/package': { name: '安装包', isDirectory: true },
      '/back/package/manage': { name: '安装包管理', isDirectory: false, parent: '/back/package' },
    } as Record<string, MenuItem>,
  }),
  getters: {
    // 获取所有菜单项
    getMenuNames: (state) => {
      return state.menus;
    },

    // 获取单个菜单项名称
    getMenuName: (state) => (path: string) => {
      return state.menus[path]?.name || path;
    },

    // 获取所有目录
    getDirectories: (state) => {
      return Object.entries(state.menus)
        .filter(([, item]) => item.isDirectory)
        .reduce((acc, [path, item]) => {
          acc[path] = item;
          return acc;
        }, {} as Record<string, MenuItem>);
    },

    // 获取某目录下的菜单项
    getMenuItemsByDirectory: (state) => (directory: string) => {
      return Object.entries(state.menus)
        .filter(([, item]) => !item.isDirectory && item.parent === directory)
        .reduce((acc, [path, item]) => {
          acc[path] = item;
          return acc;
        }, {} as Record<string, MenuItem>);
    },
  },
});